package com.godGuang.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.OSSObject;

import com.godGuang.domain.OssFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

/**
 * @ProjectName: Medic
 * @PackageName: com.medic.follow.util
 * @Description TODO
 * @Author Chen HongJie
 * @Date 2024/1/9 22:24
 * @Version 1.0
 */

@Component
public class OssUtill {
 @Autowired
 private OSSClient ossClient;

 static  String bucketName = "zhaozhenpu";

 /**
  * 上传文件到阿里云OSS
  *
  * @param file     要上传的文件
  * @param folder   文件存储的目录
  * @return 文件在OSS中的URL
  */
 public  OssFile uploadFile(MultipartFile file, String folder) throws IOException {
  String originalFilename = file.getOriginalFilename();
  if (originalFilename == null || originalFilename.isEmpty()) {
   // 文件名为空，处理异常情况，这里可以返回错误信息或抛出异常
   throw new IllegalArgumentException("上传文件名为空");
  }

  // 获取文件后缀名
  String fileExtension = "";
  int dotIndex = originalFilename.lastIndexOf(".");
  if (dotIndex > 0 && dotIndex < originalFilename.length() - 1) {
   fileExtension = originalFilename.substring(dotIndex);
  }
  //
  String simpleUUID = UUID.randomUUID().toString().replace("-", "");
  String fileName = simpleUUID + fileExtension; // 使用"ceshi"作为文件名称前缀，并加上后缀名

  //默认文件路径为/      folder文件存储路径
  String key = "";
  if (null == folder){
   key = fileName;
  }else {
   key = folder + "/" + fileName;
  }
  //开始上传时间
  long endTime = System.currentTimeMillis();

  ossClient.putObject(bucketName, key, file.getInputStream());
  Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
  GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
  request.setExpiration(expiration);
  URL url = ossClient.generatePresignedUrl(request);
  //结束上传时间
  long startTime = System.currentTimeMillis();
  //返回对象
  OssFile ossFile = new OssFile();
  //上传耗时
  ossFile.setTimeConsuming(startTime - endTime);
  //当前时间
//            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  ossFile.setFileTime(new Date());
  //folder文件存储路径
  ossFile.setFileFolder(folder);
  //文件url
  String[] split = url.toString().split("\\?");
  ossFile.setFileUrl(split[0]);
  //文件名称
  ossFile.setFileName(fileName);


  return ossFile;
 }
 /**
  * 查看文件在OSS中的URL
  *
  * @return 文件在OSS中的URL
  */
 public  String getFileUrl(String url1) {
  String key =url1;
  Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
  GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
  request.setExpiration(expiration);
  URL url = ossClient.generatePresignedUrl(request);
  return url.toString();
 }
 /**
  * 查看文件在OSS中的URL
  *
  * @param folder   文件存储的目录
  * @param fileName 文件名称
  * @return 文件在OSS中的URL
  */
 public  String getFileUrl(String folder, String fileName) {
  String key = folder + "/" + fileName;
  Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
  GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
  request.setExpiration(expiration);
  URL url = ossClient.generatePresignedUrl(request);
  return url.toString();
 }

 /**
  * 下载文件
  *
  * @param folder   文件存储的目录
  * @param fileName 文件名称
  * @return 文件流
  */
 public InputStream downloadFile(String folder, String fileName) {
  String key = folder + "/" + fileName;
  OSSObject object = ossClient.getObject(bucketName, key);
  return object.getObjectContent();
 }
}
